package com.qffx.moviebase.utils.json;

import android.text.TextUtils;
import android.util.Log;

import com.qffx.moviebase.bean.RecommendADEntity;
import com.qffx.moviebase.bean.RecommendEntity;
import com.qffx.moviebase.bean.RecommendImpl;
import com.qffx.moviebase.bean.RecommendInfo1Entity;
import com.qffx.moviebase.bean.RecommendInfo2Entity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 韩爽 on 2017/2/8.
 */

public class RecommendJSON {
    public static final String TYPE_1="1";
    public static final String TYPE_2="2";
    public static final String TAG="RecommendJSON";


    public static RecommendEntity getRecomJSON(String str){
        RecommendEntity recommendEntity = null;
        if(!TextUtils.isEmpty(str)) {
            recommendEntity= new RecommendEntity();
            try {
                JSONObject jsonObject = new JSONObject(str);
                //上面的轮播广告
                JSONArray forcusImageList = jsonObject.getJSONArray("forcusImageList");
                List<RecommendADEntity> infoList = getForcusImageList(forcusImageList);
                if (infoList == null) Log.e(TAG, "AD数据为空");
                else recommendEntity.setmRecommendAD(infoList);
                //下面的数据内容
                JSONArray infoListArray = jsonObject.getJSONArray("infoList");
                List<RecommendImpl> forcusList =getInfoList(infoListArray) ;
                if (forcusList == null) Log.e(TAG, "FORCUS数据为空");
                else recommendEntity.setmInfo(forcusList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return  recommendEntity;
    }

    //最上面的轮播广告
    private static List<RecommendADEntity> getForcusImageList(JSONArray forcusImageList) throws JSONException {
        List<RecommendADEntity> list=new ArrayList<>();
        for (int i = 0; i < forcusImageList.length(); i++) {
            JSONObject jsonObject = forcusImageList.getJSONObject(i);
            RecommendADEntity entity=new RecommendADEntity();
            entity.setId(jsonObject.getString("id"));
            entity.setForcusImageType(jsonObject.getString("forcusImageType"));
            entity.setForcusName(jsonObject.getString("forcusName"));
            entity.setSubTile(jsonObject.getString("subTitle"));
            entity.setMainTitle(jsonObject.getString("mainTitle"));
            entity.setImageUrl(jsonObject.getString("imageUrl"));
            list.add(entity);
        }
        return list;
    }

    //下面的数据
    private static List<RecommendImpl> getInfoList(JSONArray infoListArray) throws JSONException {
        List<RecommendImpl> recommendList=new ArrayList<>();
        for (int i = 0; i < infoListArray.length(); i++) {
            JSONObject jsonObject = infoListArray.getJSONObject(i);
            String objectType = jsonObject.getString("objectType");
            switch (objectType){
                case TYPE_1:
                    RecommendInfo1Entity entity_1=new RecommendInfo1Entity();
                    //object中的所有资源
                    RecommendInfo1Entity.ObjectBean objectBean=new RecommendInfo1Entity.ObjectBean();
                    JSONObject objectO_1 = jsonObject.getJSONObject("object");
                    objectBean.setId(objectO_1.getString("id"));
                    objectBean.setKeyword(objectO_1.getString("keyword"));
                    objectBean.setImgUrl(objectO_1.getString("imgUrl"));
                    objectBean.setTitle(objectO_1.getString("title"));
                    objectBean.setRecommendDate(objectO_1.getInt("recommendDate"));
                    //articleSource中的资源
                    RecommendInfo1Entity.ObjectBean.ArticleSourceBean sourceBean=new RecommendInfo1Entity.ObjectBean.ArticleSourceBean();
                    JSONObject articleSource = objectO_1.getJSONObject("articleSource");
                    sourceBean.setId(articleSource.getString("id"));
                    sourceBean.setHeadImgUrl(articleSource.getString("headImgUrl"));
                    sourceBean.setNickname(articleSource.getString("nickname"));
                     objectBean.setArticleSource(sourceBean);

                    entity_1.setObject(objectBean);
                    recommendList.add(entity_1);
                    break;
                case TYPE_2:
                    RecommendInfo2Entity entity_2=new RecommendInfo2Entity();
                    RecommendInfo2Entity.ObjectBean bean=new RecommendInfo2Entity.ObjectBean();
                    JSONObject objectO_2 = jsonObject.getJSONObject("object");
                    bean.setId(objectO_2.getString("id"));
                    bean.setVideoTime(objectO_2.getString("videoTime"));
                    bean.setCoverUrl(objectO_2.getString("coverUrl"));
                    bean.setVideoUrl(objectO_2.getString("videoUrl"));
                    bean.setTitle(objectO_2.getString("title"));
                    bean.setDescription(objectO_2.getString("description"));
                    bean.setRecommendDate(objectO_2.getInt("recommendDate"));
                    //articleSource
                    JSONObject articleSource_2 = objectO_2.getJSONObject("articleSource");
                    RecommendInfo2Entity.ObjectBean.ArticleSourceBean articleSourceBean=new RecommendInfo2Entity.ObjectBean.ArticleSourceBean();
                     articleSourceBean.setId(articleSource_2.getString("id"));
                     articleSourceBean.setNickname(articleSource_2.getString("nickname"));
                     articleSourceBean.setHeadImgUrl(articleSource_2.getString("headImgUrl"));
                    bean.setArticleSource(articleSourceBean);
                    //shareUrl
                    JSONObject shareUrl_2 = objectO_2.getJSONObject("shareUrl");
                    RecommendInfo2Entity.ObjectBean.ShareUrlBean shareUrlBean=new RecommendInfo2Entity.ObjectBean.ShareUrlBean();
                    shareUrlBean.setContent(shareUrl_2.getString("content"));
                    shareUrlBean.setTitle(shareUrl_2.getString("title"));
                    shareUrlBean.setDescription(shareUrl_2.getString("description"));
                    shareUrlBean.setImageUrlSrc(shareUrl_2.getString("imageUrlSrc"));
                    shareUrlBean.setImage(shareUrl_2.getString("image"));
                    shareUrlBean.setUrl(shareUrl_2.getString("url"));
                    bean.setShareUrl(shareUrlBean);

                    entity_2.setObject(bean);
                    recommendList.add(entity_2);
                    break;
            }
        }
        return recommendList;
    }
}
